home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 April: Mac OS SDK / Dev.CD Apr 99 SDK1.toast / Development Kits / Apple Shared Library Manager / ASLM Examples / TestTools / Sources / TestPriorityList.cp < prev    next >
Encoding:
Text File  |  1996-11-19  |  10.9 KB  |  464 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        TestPriorityList.cp
  3.  
  4.     Contains:    Tester for the Priority list class
  5.  
  6.     Copyright:    © 1991-1994 by Apple Computer, Inc., all rights reserved.
  7.  
  8. */
  9.  
  10. #ifndef __TESTPRIORITYLIST__
  11. #include "TestPriorityList.h"
  12. #endif
  13.  
  14. /**********************************************************************
  15. ** PUBLIC Constructor/Destructor
  16. ***********************************************************************/
  17.  
  18. Constructor(PriorityList)
  19. Destructor(PriorityList)
  20.  
  21. /**********************************************************************
  22. ** Test methods 
  23. ***********************************************************************/
  24.  
  25. void TTestPriorityList :: TestIsEmpty(Boolean correct) const
  26. {
  27.     if (correct)
  28.     {
  29.         if (!fTest->IsEmpty())
  30.             Printf("### ERROR: IsEmpty returned false when the list was not empty\n");
  31.     }
  32.     else
  33.     {
  34.         if (fTest->IsEmpty())
  35.             Printf("### ERROR: IsEmpty returned true when the list was empty\n");
  36.     }
  37. }
  38.  
  39. void TTestPriorityList :: TestCount(size_t shouldBe) const
  40. {
  41.     size_t is = fTest->Count();
  42.     if (is != shouldBe)
  43.         Printf("### ERROR: Count returned %u when it should have returned %u\n",
  44.                 is, shouldBe);
  45. }
  46.  
  47. void TTestPriorityList :: TestFirstLast(short iteration, short first, short last) const
  48. {
  49.     TNumber*        ptr;
  50.     TPriorityLink*    link;
  51.     
  52.     if ((ptr = (TNumber*)fTest->First()) != NULL)
  53.     {
  54.         if (ptr->fNumber != first)
  55.             if (first < 0)
  56.                 Printf("### ERROR: It#%hu: First returned #%hu when it should have returned NULL\n",
  57.                         iteration, ptr->fNumber);
  58.             else
  59.                 Printf("### ERROR: It#%hu: First returned #%hu when it should have returned #%hu\n",
  60.                         iteration, ptr->fNumber, first);
  61.     }
  62.     else
  63.     {
  64.         if (first >= 0)
  65.             Printf("### ERROR: It#%hu: First return NULL when it should have returned #%u\n",
  66.                     iteration, first);
  67.     }
  68.     if ((ptr = (TNumber*)fTest->Last()) != NULL)
  69.     {
  70.         if (ptr->fNumber != last)
  71.             if (last < 0)
  72.                 Printf("### ERROR: It#%hu: Last returned #%hu when it should have returned NULL\n",
  73.                         iteration, ptr->fNumber);
  74.             else
  75.                 Printf("### ERROR: It#%hu: Last returned #%hu when it should have returned #%hu\n",
  76.                         iteration, ptr->fNumber, last);
  77.     }
  78.     else
  79.     {
  80.         if (last >= 0)
  81.             Printf("### ERROR: It#%hu: Last return NULL when it should have returned #%u\n",
  82.                     iteration, last);
  83.     }
  84.     if ((link = (TPriorityLink*)fTest->FirstLink()) != NULL)
  85.     {
  86.         ptr = (TNumber*)link->GetValue();
  87.         if (ptr->fNumber != first)
  88.             if (first < 0)
  89.                 Printf("### ERROR: It#%hu: FirstLink returned #%hu when it should have returned NULL\n",
  90.                         iteration, ptr->fNumber);
  91.             else
  92.                 Printf("### ERROR: It#%hu: FirstLink returned #%hu when it should have returned #%hu\n",
  93.                         iteration, ptr->fNumber, first);
  94.         if (first >= 0 && link->GetPriority() != first)
  95.                 Printf("### ERROR: It#%hu: FirstLink returned priority #%hu when it should have returned #%hu\n",
  96.                         iteration, link->GetPriority(), first);
  97.     }
  98.     else
  99.     {
  100.         if (first >= 0)
  101.             Printf("### ERROR: It#%hu: FirstLink return NULL when it should have returned #%u\n",
  102.                     iteration, first);
  103.     }
  104.     if ((link = (TPriorityLink*)fTest->LastLink()) != NULL)
  105.     {
  106.         ptr = (TNumber*)link->GetValue();
  107.         if (ptr->fNumber != last)
  108.             if (last < 0)
  109.                 Printf("### ERROR: It#%hu: LastLink returned #%hu when it should have returned NULL\n",
  110.                         iteration, ptr->fNumber);
  111.             else
  112.                 Printf("### ERROR: It#%hu: LastLink returned #%hu when it should have returned #%hu\n",
  113.                         iteration, ptr->fNumber, last);
  114.         if (last >= 0 && link->GetPriority() != last)
  115.                 Printf("### ERROR: It#%hu: LastLink returned priority #%hu when it should have returned #%hu\n",
  116.                         iteration, link->GetPriority(), first);
  117.     }
  118.     else
  119.     {
  120.         if (last >= 0)
  121.             Printf("### ERROR: It#%hu: LastLink return NULL when it should have returned #%u\n",
  122.                     iteration, last);
  123.     }
  124. }    
  125.  
  126. void TTestPriorityList :: TestMember(short number, Boolean ifGone) const
  127. {
  128.     TNumber         obj(number);
  129.     TNumber*        ptr;
  130.     TPriorityLink*    link;
  131.     
  132.     if ((ptr = (TNumber*)fTest->Member(obj)) != NULL)
  133.     {
  134.         if (ifGone)
  135.             Printf("### ERROR: Member returned #%hu when it should not be present\n",
  136.                     ptr->fNumber);
  137.         else
  138.             if (number != ptr->fNumber)
  139.                 Printf("### ERROR: Member returned #%hu when it should have returned #%hu\n",
  140.                         ptr->fNumber, number);
  141.     }
  142.     else
  143.         if (!ifGone)
  144.             Printf("### ERROR: Member returned no object when #%hu should have been present\n",
  145.                     number);
  146.     if ((link = (TPriorityLink*)fTest->MemberLink(obj)) != NULL)
  147.     {
  148.         ptr = (TNumber*)link->GetValue();
  149.         if (ifGone)
  150.             Printf("### ERROR: MemberLink returned #%hu when it should not be present\n",
  151.                     ptr->fNumber);
  152.         else
  153.         {
  154.             if (number != ptr->fNumber)
  155.                 Printf("### ERROR: MemberLink returned #%hu when it should have returned #%hu\n",
  156.                         ptr->fNumber, number);
  157.             if (link->GetPriority() != ptr->fNumber)
  158.                 Printf("### ERROR: MemberLink returned priority #%hu when it should have returned #%hu\n",
  159.                         link->GetPriority(), number);
  160.         }
  161.     }
  162.     else
  163.         if (!ifGone)
  164.             Printf("### ERROR: MemberLink returned no object when #%hu should have been present\n",
  165.                     number);
  166. }    
  167.  
  168. /**********************************************************************
  169. ** PUBLIC InitTest
  170. ***********************************************************************/
  171.  
  172. void TTestPriorityList :: InitTest(BooleanParm verbose, BooleanParm, int, char**)
  173. {
  174.     short    idx;
  175.     
  176.     TStandardPool* pool = GetPool();
  177.  
  178.     for (idx = 0; idx < 100; ++idx)
  179.         fArray[idx] = NULL;
  180.         
  181.     if (pool == NULL)
  182.     {
  183.         Printf("### ERROR: There is no global pool\n");
  184.         return;
  185.     }
  186.         
  187.     fTest = new (pool) TPriorityList;
  188.     
  189.     for (idx = 0; idx < 100; ++idx)
  190.     {
  191.         TPriorityLink*    link;
  192.         TNumber*        num;
  193.         if ((num = new (pool) TNumber(idx)) == NULL)
  194.         {
  195.             Printf("### ERROR: Out of Memory at #%d\n", idx);
  196.             break;
  197.         }
  198.         if ((link = new (pool) TPriorityLink(num)) == NULL)
  199.         {
  200.             Printf("### ERROR: Out of Memory at #%d\n", idx);
  201.             break;
  202.         }
  203.         link->SetPriority(idx);
  204.         fArray[idx] = link;
  205.     }
  206.     if (idx == 100 && verbose)
  207.         Printf("INFO: Allocated 100 links and objects\n");
  208. }
  209.  
  210. /**********************************************************************
  211. ** PUBLIC RunTestIteration
  212. ***********************************************************************/
  213.  
  214. void TTestPriorityList :: RunTestIteration(BooleanParm verbose, BooleanParm)
  215. {
  216.     short            matchFirst;
  217.     short            matchLast;
  218.     short            toMatch;
  219.     short            idx, jdx;
  220.     TPriorityLink*    link;
  221.     TNumber*        ptr;
  222.     char*            str;
  223.     
  224.     if (verbose)
  225.         Printf("INFO: Testing EmptyList\n");
  226.         
  227.     TestIsEmpty(true);
  228.     TestFirstLast(-1, -1, -1);
  229.     TestCount(0);
  230.     for (jdx = 0; jdx < 100; ++jdx)
  231.         TestMember(jdx, true);
  232.         
  233.     if (verbose)
  234.         Printf("INFO: Testing AddLink, Member, Count, First, Last and IsEmpty\n");
  235.     
  236.     for (idx = 0; idx < 100; ++idx)
  237.     {
  238.         link = fArray[idx];
  239.         fTest->AddLink(link);
  240.         
  241.         TestIsEmpty(false);
  242.         TestCount(idx + 1);
  243.         TestFirstLast(idx, 0, idx);
  244.  
  245.         for (jdx = 0; jdx < 100; ++jdx)
  246.             TestMember(jdx, jdx > idx);
  247.     }
  248.     
  249.     TestIsEmpty(false);
  250.     TestCount(100);
  251.     TestFirstLast(-1, 0, 99);
  252.  
  253.     matchFirst = 0;
  254.     matchLast  = 99;
  255.     if (verbose)
  256.         Printf("INFO: Testing First, Last, RemoveFirst, RemoveLast,  and Member methods\n");
  257.  
  258.     for (idx = 0; idx < 100; ++idx)
  259.     {
  260.         TestIsEmpty(false);
  261.         TestCount(100 - idx);
  262.         TestFirstLast(idx, matchFirst, matchLast);
  263.  
  264.         for (jdx = 0; jdx < 100; ++jdx)
  265.             TestMember(jdx, jdx < matchFirst || jdx > matchLast);
  266.         
  267.         if (idx & 1)
  268.         {
  269.             link = (TPriorityLink*)fTest->RemoveFirstLink();
  270.             toMatch = matchFirst;
  271.             matchFirst += 1;
  272.             str = "RemoveFirstLink";
  273.         }
  274.         else
  275.         {
  276.             link = (TPriorityLink*)fTest->RemoveLastLink();
  277.             toMatch = matchLast;
  278.             matchLast -= 1;
  279.             str = "RemoveLastLink";
  280.         }
  281.         if (link == NULL)
  282.             Printf("### ERROR: %s return no object, instead of #%hu\n", 
  283.                    str, toMatch);
  284.         else
  285.         {
  286.             ptr = (TNumber*)link->GetValue();
  287.             if (ptr->fNumber != toMatch)
  288.                 Printf("### ERROR: %s return object #%hu, instead of #%hu\n",
  289.                        str, ptr->fNumber, toMatch);
  290.             if (link->GetPriority() != toMatch)
  291.                 Printf("### ERROR: %s return priority #%hu, instead of #%hu\n",
  292.                        str, link->GetPriority(), toMatch);
  293.         }
  294.     }
  295.     
  296.     TestIsEmpty(true);
  297.     TestFirstLast(-1, -1, -1);
  298.     TestCount(0);
  299.     for (jdx = 0; jdx < 100; ++jdx)
  300.         TestMember(jdx, true);
  301.         
  302.     if (verbose)
  303.         Printf("INFO: Testing AddLink in Random order\n");
  304.     
  305.     fTest->RemoveAll();     // Just in case
  306.     
  307.     matchFirst = 0;
  308.     matchLast  = 99;
  309.     for (idx = 0; idx < 100; ++idx)
  310.     {
  311.         if (idx & 1)
  312.             link = fArray[matchFirst];
  313.         else
  314.             link = fArray[matchLast];
  315.         fTest->AddLink(link);
  316.         
  317.         TestIsEmpty(false);
  318.         TestCount(idx + 1);
  319.         if (idx == 0)
  320.             TestFirstLast(idx, 99, 99);
  321.         else
  322.             TestFirstLast(idx, 0, 99);
  323.  
  324.         for (jdx = 0; jdx < 100; ++jdx)
  325.             if (idx & 1)
  326.                 TestMember(jdx, jdx > matchFirst && jdx <= matchLast);
  327.             else
  328.                 TestMember(jdx, jdx >= matchFirst && jdx < matchLast);
  329.         if (idx & 1)
  330.             matchFirst += 1;
  331.         else
  332.             matchLast -= 1;
  333.     }
  334.     
  335.     TestIsEmpty(false);
  336.     TestCount(100);
  337.     TestFirstLast(-1, 0, 99);
  338.  
  339.     if (verbose)
  340.         Printf("INFO: Testing Remove by pointer\n");
  341.         
  342.     matchFirst = 49;
  343.     matchLast  = 50;
  344.     
  345.     for (idx = 0; idx < 100; ++idx)
  346.     {
  347.         if (idx & 1)
  348.             jdx = matchFirst;
  349.         else
  350.             jdx = matchLast;
  351.         link = fArray[jdx];
  352.         
  353.         TestIsEmpty(false);
  354.         TestCount(100 - idx);
  355.         if (idx != 99)
  356.             TestFirstLast(idx, 0, 99);
  357.         else
  358.             TestFirstLast(idx, 0, 0);
  359.         
  360.         if (!fTest->Remove(link->GetValue()))
  361.             Printf("ERROR: Remove Link #%u Failed\n", jdx);
  362.  
  363.         TestCount(99-idx);
  364.         if (idx < 98)
  365.             TestFirstLast(idx, 0, 99);
  366.         else
  367.             if (idx == 98)
  368.                 TestFirstLast(idx, 0, 0);
  369.                 
  370.         if (idx & 1)
  371.             matchFirst -= 1;
  372.         else
  373.             matchLast += 1;
  374.             
  375.     }
  376.     if (verbose)
  377.         Printf("INFO: Testing Remove by reference\n");
  378.         
  379.     fTest->RemoveAll();            // Just in case
  380.     
  381.     for (idx = 0; idx < 100; ++idx)
  382.     {
  383.         link = fArray[idx];
  384.         fTest->AddLink(link);
  385.     }
  386.     
  387.     TestIsEmpty(false);
  388.     TestCount(100);
  389.     TestFirstLast(-1, 0, 99);
  390.     for (jdx = 0; jdx < 100; ++jdx)
  391.         TestMember(jdx, false);
  392.  
  393.     matchFirst = 49;
  394.     matchLast  = 50;
  395.  
  396.     for (idx = 0; idx < 100; ++idx)
  397.     {
  398.         if (idx & 1)
  399.             jdx = matchFirst;
  400.         else
  401.             jdx = matchLast;
  402.         link = fArray[jdx];
  403.         
  404.         TestIsEmpty(false);
  405.         TestCount(100 - idx);
  406.         if (idx != 99)
  407.             TestFirstLast(idx, 0, 99);
  408.         else
  409.             TestFirstLast(idx, 0, 0);
  410.         
  411.         if (!fTest->Remove(*(TNumber*)link->GetValue()))
  412.             Printf("ERROR: Remove Link #%u Failed\n", jdx);
  413.  
  414.         TestCount(99-idx);
  415.         if (idx < 98)
  416.             TestFirstLast(idx, 0, 99);
  417.         else
  418.             if (idx == 98)
  419.                 TestFirstLast(idx, 0, 0);
  420.                 
  421.         if (idx & 1)
  422.             matchFirst -= 1;
  423.         else
  424.             matchLast += 1;
  425.             
  426.     }
  427.  
  428.     if (verbose)
  429.         Printf("INFO: Testing DeleteAll\n");
  430.     
  431.     fTest->RemoveAll();            // Just in case
  432.     
  433.     for (idx = 0; idx < 100; ++idx)
  434.     {
  435.         link = fArray[idx];
  436.         fTest->AddLink(link);
  437.     }
  438.     
  439.     fTest->DeleteAll(kTDynamicPointer);
  440.     
  441.     TestIsEmpty(true);
  442.     TestCount(0);
  443.     TestFirstLast(-1, -1, -1);
  444.     for (jdx = 0; jdx < 100; ++jdx)
  445.         TestMember(jdx, true);
  446. }
  447.  
  448. /**********************************************************************
  449. ** PUBLIC EndTest
  450. ***********************************************************************/
  451.  
  452. void TTestPriorityList :: EndTest(BooleanParm verbose, BooleanParm)
  453. {
  454.     if (verbose)
  455.         Printf("INFO: End of Priority List test\n");
  456.     for (short idx = 0; idx < 100; ++idx)
  457.     {
  458.         TPriorityLink* link = fArray[idx];
  459.         delete link;
  460.     }
  461.     if (verbose)
  462.         Printf("INFO: Deleted 100 links\n");
  463. }
  464.